package com.wzy.authority.mapper;

import com.wzy.authority.base.result.Results;
import com.wzy.authority.domain.SysPermission;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface SysPermissionMapper {
    /**
     * 根据主键删除权限
     * @param id
     * @return
     */
    int deleteByPrimaryKey(Integer id);

    /**
     * 插入一条权限数据
     * @param record
     * @return
     */
    int insert(SysPermission record);


    /**
     * 根据主键查询数据
     * @param id
     * @return
     */
    SysPermission selectByPrimaryKey(Integer id);

    /**
     * 根据主键  修改数据(存在数据就修改)
     * @param record
     * @return
     */
    int updateByPrimaryKeySelective(SysPermission record);

    /**
     * 全部修改,不存在就置为null
     * @param record
     * @return
     */
    int updateByPrimaryKey(SysPermission record);

    /**
     * 获取所有数据
     * @return
     */
    List<SysPermission> getAllMenuList();


    /**
     * 根据角色id获取所有的规则
     * @param roleId
     * @return
     */
    @Select("select p.* from sys_permission p , sys_role_permission rp where p.id = rp.permissionId and rp.roleId = #{roleId}")
    List<SysPermission> selectByRoleId(@Param("roleId") Integer roleId);

    /**
     * 删除id为permissionId的并且删除所有的下级菜单,所以删除parentId为permissionId的数据
     * @param permissionId
     * @return
     */
    @Delete("delete from sys_permission where id = #{permissionId} or parentId = #{permissionId}")
    Integer deleteByPermissionId(Integer permissionId);

    /**
     * 通过用户id获取菜单列表
     * @param userId
     * @return
     */
    @Select("SELECT sp.* FROM `sys_role_user` sru" +
            " inner join `sys_role_permission`  srp" +
            " on sru.roleId = srp.roleId" +
            " left join `sys_permission` sp" +
            " on srp.permissionId = sp.id" +
            " where sru.userId = #{userId} order by sort")
    List<SysPermission> getAllMenuListByUserId(Integer userId);
}